package com.imooc.controller;

import com.imooc.eo.SearchResumesEO;
import com.imooc.result.GraceJSONResult;
import com.imooc.service.ResumeSearchService;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liansen
 * @create 06-15-23:35
 */
@RestController
@RequestMapping("/resumeSearch")
public class ResumeSearchController {

    @Autowired
    private ElasticsearchRestTemplate esTemplate;

    @Autowired
    private ResumeSearchService resumeSearchService;

    /**
     * 刷新简历后，将简历刷进es中 (测试的时候直接测这个方法)
     * @param userId
     * @return
     */
    @GetMapping("/flush")
    public GraceJSONResult flush(String userId){
        resumeSearchService.flashformAnfFlush(userId);
        return GraceJSONResult.ok();
    }

    //查询所有(分页)
    @GetMapping("/match_all")
    public GraceJSONResult matchAll(Integer page, Integer pageSize){
        if(page < 1){
            page = 1;
        }
        page--;
        Pageable pageable = PageRequest.of(page, pageSize);

        Query query = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchAllQuery())
                                                    .withPageable(pageable)
                                                    .build();
        SearchHits<SearchResumesEO> searchHits = esTemplate.search(query, SearchResumesEO.class);
        List<SearchResumesEO> collect = searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());
        return GraceJSONResult.ok(collect);
    }

    //多条件查询
    @GetMapping("/term")
    public GraceJSONResult term(){
        Query query = new NativeSearchQueryBuilder().withQuery(QueryBuilders.termQuery("sex", 2))
                                                    .withQuery(QueryBuilders.matchQuery("nickname", "陈平安"))
                                                    .build();
        SearchHits<SearchResumesEO> searchHits = esTemplate.search(query, SearchResumesEO.class);
        List<SearchResumesEO> list = getList(searchHits, SearchResumesEO.class);
        return GraceJSONResult.ok(list);
    }


    /**
     *      将这段代码抽取成泛型方法供别人调用
     *         List<SearchResumesEO> list = new ArrayList<>();
     *         for (SearchHit<SearchResumesEO> searchHit : searchHits) {
     *             SearchResumesEO content = searchHit.getContent();
     *             list.add(content);
     *         }
     * @param searchHits
     * @param clazz
     * @param <T>
     * @return
     */

    private <T> List<T> getList(SearchHits<T> searchHits, Class<T> clazz ){
        List<SearchHit<T>> hits = searchHits.getSearchHits();
        List<T> list = new ArrayList<>();
        for (SearchHit<T> hit : hits) {
            T content = hit.getContent();
            list.add(content);
        }
        return list;
    }

}
